package cn.edu.Day11.login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.security.PublicKey;

public class LoginFrame extends JFrame {
    public static final Toolkit KIT = Toolkit.getDefaultToolkit();

    public static final int W = 470;
    public static final int H = 650;

    public LoginFrame(){
        this.setTitle("注册/登陆");
        int x = (KIT.getScreenSize().width-W)/2;
        int y = (KIT.getScreenSize().height-H)/2;
        this.setBounds(x,y,W,H);
        LoginPanel lp = new LoginPanel();
        this.add(lp);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String[] args) {
        new LoginFrame();
    }
}

class LoginPanel extends JPanel{

    JTextField jft1 = new JTextField();
    JTextField jft2 = new JTextField();
    JTextField jft3 = new JTextField();
    JTextField jft4 = new JTextField();

    JButton jbtLog = new JButton("登陆");
    JButton jbtReg = new JButton("注册");

    File file ;

    public LoginPanel(){
        file = new File("src/resources/in.txt");
        this.setLayout(null);
        setDaXiao();
        jft1.setLocation(120,30);
        jft2.setLocation(120,60);
        jbtReg.setLocation(120,120);
        jft3.setLocation(120, 300);
        jft4.setLocation(120,330);
        jbtLog.setLocation(120,390);
        jbtReg.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                User user = new User();
                user.setName(jft1.getText());
                user.setPassword(jft2.getText());
                ObjectOutputStream oos = null;
                try {
                    oos = new ObjectOutputStream(new FileOutputStream(file));
                    oos.writeObject(user);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }finally {
                    try {
                        oos.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
                System.out.println("用户信息序列化完毕");
            }
        });

        jbtLog.addActionListener((e)->{
            //获取用户的输入内容
            String userStr = jft3.getText();
            String pwdStr = jft4.getText();
            //从反序列化中获取对象.
            try {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
                User user = (User) ois.readObject();
                if(userStr.equals(user.getName())&&pwdStr.equals(user.getPassword())){
                    System.out.println("用户登陆成功");
                }else{
                    System.out.println("用户登陆失败");
                }
            } catch (IOException | ClassNotFoundException ex) {
                ex.printStackTrace();
            }

        });


        this.add(jft1);
        this.add(jft2);
        this.add(jft3);
        this.add(jft4);
        this.add(jbtReg);
        this.add(jbtLog);


    }
    void setDaXiao(){
        jft1.setSize(200,30);
        jft2.setSize(200,30);
        jft3.setSize(200,30);
        jft4.setSize(200,30);
        jbtReg.setSize(200,30);
        jbtLog.setSize(200,30);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.setBackground(Color.BLACK);

    }

}
